<template>
<div style="text-align: left;">
    <div style="margin-bottom:20px">
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商店管理</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/index/shoplist' }">商店列表</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <el-input
        placeholder="请输入内容"
        v-model.trim="searchMsg"
        v-on:input="fetchGoods"
        style="width:400px">
        <i slot="prefix" class="el-input__icon el-icon-search" id="search_input"></i>
    </el-input>

     <el-table
    stripe
    v-loading="loading"
    ref="multipleTable"
    :data="goods"
    tooltip-effect="dark"
    style="width: 100%"
    height="550"
    @selection-change="handleSelectionChange"
    >
    <el-table-column
      type="selection"
      width="55">
    </el-table-column>
    <el-table-column
      prop="goodsid"
      label="id"
      sortable
      width="120">
      <!-- <template slot-scope="scope">{{ scope.row.id }}</template> -->
    </el-table-column>
    <el-table-column
      prop="name"
      label="商品名"
      sortable
      width="120">
    </el-table-column>
    <el-table-column
      label="类型"
      prop="type"
      sortable
      width="120">
    </el-table-column>
    <el-table-column
      prop="price"
      label="价格"
      sortable
      width="120">
    </el-table-column>
    <el-table-column
      prop="num"
      label="数量"
      sortable
      width="120">
    </el-table-column>
    <el-table-column
      label="图片"
      show-overflow-tooltip>
      <template slot-scope="scope">
          <el-image :src="scope.row.picaddress" style="height:120px"/>
      </template>
    </el-table-column>
    
    <el-table-column
        fixed="right"
        label="操作"
        width="150">
        <template slot-scope="scope">
            <el-button @click.native.stop="deleteGoods(scope.row)" type="danger" size="mini" >删除</el-button>
            <el-button size="mini" @click.native.stop="editButtonClick(scope.row)">编辑</el-button>
        </template>
    </el-table-column>
  </el-table>

  <el-row :gutter="10" style="margin-top:20px">
    <el-col :span="2">
          <el-button type="danger" size="small" :disabled="multipleSelection.length==0? true:false" @click="deleteSelect()">删除所选</el-button>
    </el-col>
    <el-col :span="2">
        <goodsCreate v-bind="shopId"></goodsCreate>
    </el-col>
    <el-col :span="2">
        <typeCreate></typeCreate>
    </el-col>
    <el-col :span="2">
        <el-button type="success" size="small" @click="flash()">刷新</el-button>
    </el-col>

	<el-rol :span="2">
		  <div class="block">
			<el-pagination
			@current-change="handleCurrentChange"
			:current-page="currentPage"
			:page-size="10"
			layout="total, prev, pager, next"
			:total="total">
			</el-pagination>
		</div>
	</el-rol>
</el-row>

   <el-dialog title="修改商品" :visible.sync="dialogFormVisible">
        <el-form :model="selectedGoods">
          <el-form-item label="商品名" :label-width="formLabelWidth">
            <el-input v-model="selectedGoods.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="价格" :label-width="formLabelWidth">
            <el-input v-model="selectedGoods.price" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="数量" :label-width="formLabelWidth">
            <el-input v-model="selectedGoods.num" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="图片地址" :label-width="formLabelWidth">
            <el-input v-model="selectedGoods.picaddress" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="类型">
              <el-radio-group v-model="selectedGoods.type"  @change="radioSelectChange">
                <el-radio  :label="item.type_name" :key="item.type" v-for="item in types"></el-radio>
              </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editGoods()">确 定</el-button>
        </div>
      </el-dialog>


</div>
</template>

<script>
import goodsCreate from './goodsCreate.vue'
import typeCreate from './typeCreate.vue'
export default {
	name:"goodsList",
	components:{goodsCreate,typeCreate},
    data(){
        return{
            shopId:'',
			      dialogFormVisible:false,
			      multipleSelection: [],
            loading:true,
            goods:[],
            searchMsg:'',
            currentPage:1,
            total:0,
			      selectedGoods:{},
            types:[],
        }
    },
    mounted(){
        this.getType()
        this.shopId = this.$route.query.id
        this.getGoodsList()
    },
    methods:{

      flash(){
        this.getGoodsList()
        this.getType()
      },

      getType(){
        this.$axios.get('getAllTypes').then(resp=>{
          if(resp.status==200){
            this.types = resp.data.data
            console.log(this.types)
          }
          
        })
      },

        fetchGoods(){
          this.currentPage = 1
          clearTimeout(this.timer);
          this.timer=setTimeout(()=>{
            this.getGoodsList()
          },800);
        },

        getGoodsList(){
			      this.loading = true
            this.$axios.get('listGoodsBySidAndMSg',{
                params:{
                    id:this.shopId,
                    searchMsg:this.searchMsg,
                    currentPage:this.currentPage
                }
            }).then(resp => {
                if(resp.status==200){
                    this.goods = resp.data.data
                    this.total = resp.data.total
                    this.loading = false
                    for(var i=0;i<this.goods.length;i++){
                      if(this.goods[i].type1.type_name!=null){
                        this.goods[i].type = this.goods[i].type1.type_name
                      }
                    }
                }
            })
        },

        radioSelectChange(val){
          console.log(val)
          console.log(this.selectedGoods.type)
          this.$forceUpdate()
        },

		deleteGoods(goods){
			this.$axios.get('deleteGoodsById',{
				params:{
					id:goods.goodsid
				}
			}).then(resp=>{
				if(resp.status==200){
					this.$message.success("删除成功")
					this.getGoodsList()
				}
			})
		},
		editButtonClick(goods){
            //解决浅拷贝
            var _goods = JSON.stringify(goods)
            this.selectedGoods = JSON.parse(_goods)
            this.radio = this.selectedGoods.type
            this.dialogFormVisible = true
            this.$forceUpdate()
        },
        editGoods(){
          for(var i=0;i<this.types.length;i++){
            if(this.selectedGoods.type==this.types[i].type_name){
              this.selectedGoods.type = this.types[i].type
              break;
            }
          }
          console.log(this.selectedGoods.type)
          this.$axios.post('/editGoods',this.selectedGoods).then(resp=>{
            if(resp.status==200){
                this.$message({message:"修改成功",type:'success'})
                this.dialogFormVisible=false
                this.getGoodsList()
            }
          })
        },
		deleteSelect(){
          for(let item of this.multipleSelection){
            this.deleteGoods(item)
          }
        },

    }

}
</script>

<style>

</style>